#!/bin/bash


root_path=`dirname "$0"`

to_exit() {
    echo "$1"
    exit "$2"
}

apt_install() {
    for package in "$@"
    do
        dpkg -s "$package" &> /dev/null
        if [ ! "$?" -eq 0 ]; then
            sudo apt-get install "$package" -y &> /dev/null && echo "$package 安装成功" || to_exit "$package 安装失败,请检查" 2
        else
            echo "$package 已安装,无需处理"
        fi
    done
}

config_file() {
    cp "$root_path"/bashrc ~/.bashrc
    cp "$root_path"/inputrc ~/.inputrc
    cp "$root_path"/gitconfig ~/.gitconfig
    if [ `tmux -V | cut -d ' ' -f 2 -s | awk '{print int($0)}'` -ge 2 ]; then
        cp "$root_path"/tmux2.conf ~/.tmux.conf
    else
        cp "$root_path"/tmux1.conf ~/.tmux.conf
    fi
    echo -e '~/.bashrc\t配置成功\n~/.inputrc\t配置成功\n~/.gitconfig\t配置成功\n~/.tmux.conf\t配置成功'
}

aliyun_repo() {
    sudo mv /etc/apt/sources.list /etc/apt/sources.list.bak
    sudo cp sources.list /etc/apt/sources.list
    echo "更新软件包列表..."
    sudo apt-get update &> /dev/null
    echo "阿里源配置成功"
}

auto_config() {
    aliyun_repo
    apt_install tmux exuberant-ctags cscope vim-gnome flameshot astyle tidy sqlformat
    config_file
}

${1-auto_config}
